define([
        "core",
        "jQueryFancyTree"
    ], function (core) {
        var permission = {};
        permission.treeUrl = "";
        permission.formSelector = "#recordForm";
        permission.resetForm = function (menu) {
            core.resetForm(permission.formSelector, menu);
        }

        function showMessage(msg) {
            if (core.isSuccess(msg)) {
                core.noticeMessage(msg);
                loadMenuTree();
            }
        }

        function loadMenuTree() {
            $(".list-tree").fancytree({
                selectMode: 2,
                source: {url: permission.treeUrl, debugDelay: 100},
                click: function (event, data) {
                    var menu = data.node.data;
                    permission.resetForm(menu);
                }
            });
            permission.resetForm();
        };

        permission.initPermissionPage = function (treeUrl, actionBaseUrl) {
            var actionUrl = new core.Urls(core.getUrl(actionBaseUrl));
            this.treeUrl = core.getUrl(treeUrl);
            loadMenuTree();

            $(".parent-tree").on("click", function (e) {
                $("#modalBody").fancytree({
                    selectMode: 2,
                    source: {url: treeUrl, debugDelay: 100},
                    click: function (event, data) {
                        var p = data.node.data;
                        $("[name=parentCode]").val(p.code);
                        $("[name=parentName]").val(p.name);
                    }
                });

                $("#modal").modal();
            });

            $(".action-insert").on("click", function (e) {
                $("input[name='id']").val("");
                core.validator("#recordForm",function(){
                    $.ajax({
                        url: actionUrl.insert(),
                        type: "post",
                        data: $("#recordForm").serialize(),
                        dataType: "json",
                        success: showMessage
                    })
                })
            });

            $(".action-update").on("click", function (e) {
                core.validator("#recordForm",function() {
                    $.ajax({
                        url: actionUrl.update(),
                        type: "post",
                        data: $("#recordForm").serialize(),
                        dataType: "json",
                        success: showMessage
                    })
                })
            });

            $(".action-delete").on("click", function (e) {
                var url = actionUrl.delete($(permission.formSelector + " [name=id]").val());
                $.ajax({
                    url: url,
                    type: "delete",
                    dataType: "json",
                    success: showMessage
                })
            });
        }

        return permission;
    }
);